package org.NooLab.itexx.app.controller;

import java.util.ArrayList;

import org.NooLab.itexx.FoundResultsGuiUpdater;
import org.NooLab.itexx.ITexxWindowCommons;
import org.NooLab.itexx.app.controller.analyzer.SerDocumentScoring;
import org.NooLab.itexx.app.controller.results.SearchEngineResultItem;
import org.NooLab.utilities.logging.PrintLog;




/**
 * 
 * this class (and process) is hosted by SessionManager, from which it gets its data
 * 
 * the data are SER items with scoring, packaged into an object of "SerDocumentScoring"
 * this contains the scoring results as well as a reference to the serItem
 * (ser = SearchEngineResult)
 * 
 * here, we also maintain the global Top-N - list of items to be displayed;
 * 
 * that is, we also do the constantly performed inserting and sorting, upon which
 * the whole list is handed to the "resultsGuiUpdater"
 * The resultsGuiUpdater just takes that list, prepares a small html and displays
 * it on the panel 
 *
 *
 */
public class ScoredSerDocuments implements ScoredSerDocumentsIntf{

	SessionManager sessionManager;
	private ITexxWindowCommons appCommons;
	
	
	ArrayList<ScoredSerDocument> items = new ArrayList<ScoredSerDocument>(); 
	
	FoundResultsGuiUpdater resultsGuiUpdater;
	FoundListGenerator foundListGenerator;  
	
	boolean listServiceIsRunning = false;
	ListService listService;
	
	private PrintLog out;

	// ========================================================================
	public ScoredSerDocuments(SessionManager smgr) {
		// 
		sessionManager = smgr;
		appCommons = sessionManager.appCommons ;
		out = appCommons.out ;
		
		resultsGuiUpdater = appCommons.getResultsGuiUpdater(); // is it ok? => !=null ?
		
		listService = new ListService(); // starts right away...
	}
	// ========================================================================

	public void close(){
		listServiceIsRunning = false;
		out.delay(160);
	}
	
	public void add( SerDocumentScoring scoredResultItem) {
		// 
		SearchEngineResultItem serItem;
		ScoredSerDocument item = new ScoredSerDocument(this);
		
		serItem = scoredResultItem.getSer() ;
		
		item.setRequestUrl("");
		item.setDocumentUrl("");
		item.setScore(1.0);
		item.setHotSpots( new ArrayList<String>()) ;
		item.setRequestGuid("");
		item.setPackageGuid(""); // these link us to the RequestHistory
		
		
		items.add( item );
	}

	public ITexxWindowCommons getAppCommons() {
		return appCommons;
	}

	public ArrayList<ScoredSerDocument> getItems() {
		return items;
	}

	
	// ....................................................
	class ListService implements Runnable, GeneralQueueServiceIntf{

		Thread smgrListSrvThrd;
		FoundListGenerator foundListGenerator;
		
		public ListService(){
			
			foundListGenerator = new FoundListGenerator(sessionManager, (GeneralQueueServiceIntf)this) ;
			
			smgrListSrvThrd = new Thread(this,"smgrListSrvThrd");
			smgrListSrvThrd.start();
		}
		
		
		@Override
		public void run() {
			boolean isWorking=false, isWaiting;
			listServiceIsRunning = true;
	
			while (listServiceIsRunning){
				
				
				if (isWorking==false){
					isWorking=true;
					isWaiting=true;
					for (int w=0;w<400;w++){ // 20 seconds pause between treatments
						out.delay(50);
						if (listServiceIsRunning==false){
							isWaiting=false;
							break;
						}
					}// -> waiting in small steps
					isWaiting=false;
					
					// ..........................
					if (listServiceIsRunning){
						// this will open its own thread, which however will
						// call back to influence our looping indicator "listServiceIsRunning"
						// call back is through GeneralQueueServiceIntf
						// we also could regulate the number of parallel processes this way...
						//...................
						
						// we only need to work on  it if there was a change
						// (but there always will be
						
						
						foundListGenerator.update() ;
						
						
					} // ?
					
					isWorking=false;
				}
				else{
					out.delay(100) ;
				}
				
				
			}//->
			
			out.print(2, "\nListService for digesting items before display (class <ScoredSerDocuments>) stopped working.\n") ;
			listServiceIsRunning = false;
		}


		@Override
		public void setSubProcessFinished(boolean flag) {
			// 
			listServiceIsRunning = false;
		}


		@Override
		public void setSubProcessFailed(boolean flag) {
			// 
			listServiceIsRunning = false;
		}
		
		
		
	}
	// ....................................................
	
	
}
